<?php

/**
 * Configuration for: Error reporting
 * Useful to show every little problem during development, but only show hard errors in production
 */
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
require_once 'SBWord.php';

/**
 * This is the "base controller class". All other "real" controllers extend this class.
 */
class Indexer {

    protected $connection;
    protected $Dictionary = array();

    /**
     * Searches for all the words from our Dictionary in this Document
     * @param String $documentData
     * @return Integer 
     */
    function processDocument($documentData, $url) {
        $Index = 0;
        
        $iFound = $this->checkURL($url);
        if ($iFound == 0) {
            return $Index;
        }
        $this->openDBConnection();
        foreach ($this->Dictionary as $dictWord) {
            $Log = 'Finding Word : ' . $dictWord->getMWord() . ' in : ' . $url . PHP_EOL;
            $Index = $this->findWord($documentData, $dictWord->getMWord());
            $Log .= 'Found at : ' . $Index . PHP_EOL;
            if ($Index > 0) {
                //echo 'Found at : ' . $Index;
                $this->indexData($iFound, $dictWord, $Index);
            }
            // Write the contents to the file, 
            // using the FILE_APPEND flag to append the content to the end of the file
            // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
            file_put_contents('/home/ashwitha/public_html/spidercrawler/trunk/SpiderBot/Crawler/Logs.txt', $Log, FILE_APPEND | LOCK_EX);
        }
        $this->closeDBConnection();
        return $Index;
    }

    /**
     * Searches for a given word in the Document Data
     * @param String $documentData Text data of the HTML Document
     * @param String $word Word to be searched for
     * @return Integer The position where the word was found
     */
    function findWord($documentData, $word) {
        $Index = -1;
        if (stripos($documentData, $word)) {
            $Index = stripos($documentData, $word);
        }
        return $Index;
    }

    /**
     * Opens up a Database connection and sets the $connection variable to hold this connection
     */
    function openDBConnection() {
        $this->connection = mysqli_connect("localhost", "admin", "admin"); //database connection
        mysqli_select_db($this->connection, "dbspiderbot");
    }

    /**
     * Closes an Open database connection
     */
    function closeDBConnection() {
        mysqli_close($this->connection);
    }

    /**
     * Time to index this data into the Database
     * @param String $URLID Address of the link where 
     * @param String $Word Word to index this document against
     * @param type $Position Position of the word in this Document
     */
    function indexData($URLID, $Word, $Position) {
        $query = "INSERT INTO `dbspiderbot`.`tblurldatamap` (`URLID`, `DictID`, `Offset`) VALUES (?, ?, ?)";
        //echo $query;
        /* create a prepared statement */
        if ($stmt = mysqli_prepare($this->connection, $query)) {

            //echo "URLID - " . $URLID . "   WordID - " . $Word->getMiID() . "  Position - " . $Position;
            /* bind parameters for markers */
            $stmt->bind_param("iii", $URLID, $Word->getMiID(), $Position);

            /* execute query */
            $stmt->execute();

            /* close statement */
            $stmt->close();
        }
    }

    /**
     * Fill our Dictionary of Words from the Database
     */
    function fillDictionary() {
        //echo "in fillDict function<br/>";
        if (empty($this->Dictionary)) {
            //echo 'Dictionary is empty<br/>';
            $this->openDBConnection();
            $Query = "SELECT ID,Word FROM tbldictionary";
            $ResultSet = mysqli_query($this->connection, $Query);
            while ($row = mysqli_fetch_array($ResultSet)) {
                array_push($this->Dictionary, SBWord::withRow($row));
                //echo $row['ID'] . " " . $row['Word'];
                //echo "<br/>";
            }
            $this->closeDBConnection();
        }
        //echo "fillDict function done<br/>";
    }

    /**
     * Checks whether the passed URL is eligible for Indexing OR NOT.
     * @param type $URL
     * @return Integer 0 - If it is not found, ID of the URL if it IS found.
     */
    public function checkURL($URL) {
        $bStatus = 0;
        $this->openDBConnection();
        $query = "SELECT * from tblurlrecorder where `Date` between DATE_ADD(NOW(), INTERVAL -30 DAY) and NOW() AND URL LIKE '" . $URL . "'";
        //echo $query;
        $ResultSet = mysqli_query($this->connection, $query);
        if ($row = mysqli_fetch_array($ResultSet)) {
            // do nothing
        } else {
            $query = "INSERT INTO `dbspiderbot`.`tblurlrecorder` (`ID`, `URL`, `isCrawled`, `Date`, `ReferredBy`, `Title`, `Description`) VALUES (NULL, ?, b'1', CURRENT_TIMESTAMP, '', NULL, NULL)";
            /* create a prepared statement */
            if ($stmt = mysqli_prepare($this->connection, $query)) {

                /* bind parameters for markers */
                $stmt->bind_param("s", $URL);

                /* execute query */
                $stmt->execute();

                //Set the return ID to the LAST entered ROW's ID
                $bStatus = mysqli_insert_id($this->connection);
                
                /* close statement */
                $stmt->close();
            }
        }
        $this->closeDBConnection();
        return $bStatus;
    }

    public function __construct() {
        
    }

}

//$Indexer = new Indexer();
//$Indexer->fillDictionary();
//echo '<br/>Index : ' . $Indexer->processDocument("Hypertext Preprocessor Downloads Documentation Get Involved Help Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search (current page) / Focus search box PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world. Download 5.5.12 · Release Notes 5.4.28 · Release Notes 5.3.28 · Release Notes 02 May 2014 PHP 5.6.0beta2 Released The PHP development team announces the immediate availability of PHP 5.6.0beta2. As we entered the feature freeze with beta1, this release is almost exclusively a bugfix-only release. All users of PHP are encouraged to test this version carefully, and report any bugs in the bug tracking system. THIS IS A DEVELOPMENT PREVIEW - DO NOT USE IT IN PRODUCTION! PHP 5.6.0beta2 comes with a small number of new features, including: Watchpoint support for phpdbg A new fetching mode to mysqlnd which uses less memory but implies more memory copy For more information about the new features you can check out the work-in-progress documentation or you can read the full list of changes in the NEWS file contained in the release archive. For source downloads of PHP 5.6.0beta2 please visit the download page. Windows binaries can be found on windows.php.net/qa/. Our third beta should show up on the 15th of May. Thank you for helping us make PHP better. 01 May 2014 PHP 5.4.28 Released The PHP development team announces the immediate availability of PHP 5.4.28. 19 bugs were fixed in this release, including CVE-2014-0185. All PHP 5.4 users are encouraged to upgrade to this version. For source downloads of PHP 5.4.28 please visit our downloads page, Windows binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog. 30 Apr 2014 PHP 5.5.12 Released The PHP Development Team announces the immediate availability of PHP 5.5.12. This release fixes several bugs against PHP 5.5.11, as well as CVE-2014-0185 regarding PHP-FPM. All PHP users are encouraged to upgrade to this new version. For source downloads of PHP 5.5.12, please visit our downloads page. Windows binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog. 11 Apr 2014 PHP 5.6.0beta1 Released The PHP development team announces the immediate availability of PHP 5.6.0beta1. This release adds new features and fixes bugs and marks the feature freeze for the PHP 5.6.0 release. All users of PHP are encouraged to test this version carefully, and report any bugs in the bug tracking system. THIS IS A DEVELOPMENT PREVIEW - DO NOT USE IT IN PRODUCTION! PHP 5.6.0beta1 comes with a number of new features, including: A new method called fread() to the SplFileObject class A new static method called createFromMutable() to the DateTimeImmutable class A new function called hash_equals() Support for marks to the PCRE extension Support for asynchronous connections and queries to the Pgsql extension For more information about the new features you can check out the work-in-progress documentation or you can read the full list of changes in the NEWS file contained in the release archive. For source downloads of PHP 5.6.0beta1 please visit the download page. Windows binaries can be found on windows.php.net/qa/. Our second beta should show up on the 24th of April. Thank you for helping us make PHP better. 03 Apr 2014 PHP 5.4.27 Released The PHP development team announces the immediate availability of PHP 5.4.27. 6 bugs were fixed in this release, including CVE-2013-7345. All PHP 5.4 users are encouraged to upgrade to this version. For source downloads of PHP 5.4.27 please visit our downloads page, Windows binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog. 02 Apr 2014 PHP 5.5.11 Released The PHP development team announces the immediate availability of PHP 5.5.11. Several bugs were fixed in this release, some bundled libraries updated and a security issue has been fixed : CVE-2013-7345. We recommand all PHP 5.5 users to upgrade to this version. For source downloads of PHP 5.5.11 please visit our downloads page, Windows binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog. 20 Nov 2013 Our modern web theme goes live! The PHP web team are delighted to announce the launch of the new web theme that has been in beta for many months. Lots of hard work has gone into this release and we will be continually improving things over time now that we have migrated away from the legacy theme. From an aesthetics point of view the general color scheme of the website has been lightened from the older dark purple. Lots of borders and links use a similar purple color to attain consistency. Fonts are smoother, and colors, contrast and highlighting have significantly improved; especially on function reference pages. Code examples should now be much more readable. The theme is marked up using HTML5 and is generally much more modern. We are using Google Fonts and Bootstrap for our theme base. To provide valuable feedback, you can use the 'Feedback' widget on the side of the page (not visible on smartphones) and to report bugs, you can make use of the bugs.php.net tracker. Despite our extensive multi-device/multi-browser testing, we may have missed something. So, if you spot any issues please do get in touch. Special thanks to the guys who helped make this happen, you know who you are! 24 Oct 2013 A further update on php.net We are continuing to work through the repercussions of the php.net malware issue described in a news post earlier today. As part of this, the php.net systems team have audited every server operated by php.net, and have found that two servers were compromised: the server which hosted the www.php.net, static.php.net and git.php.net domains, and was previously suspected based on the JavaScript malware, and the server hosting bugs.php.net. The method by which these servers were compromised is unknown at this time. All affected services have been migrated off those servers. We have verified that our Git repository was not compromised, and it remains in read only mode as services are brought back up in full. As it's possible that the attackers may have accessed the private key of the php.net SSL certificate, we have revoked it immediately. We are in the process of getting a new certificate, and expect to restore access to php.net sites that require SSL (including bugs.php.net and wiki.php.net) in the next few hours. To summarise, the situation right now is that: JavaScript malware was served to a small percentage of php.net users from the 22nd to the 24th of October 2013. Neither the source tarball downloads nor the Git repository were modified or compromised. Two php.net servers were compromised, and have been removed from service. All services have been migrated to new, secure servers. SSL access to php.net Web sites is temporarily unavailable until a new SSL certificate is issued and installed on the servers that need it. Over the next few days, we will be taking further action: php.net users will have their passwords reset. Note that users of PHP are unaffected by this: this is solely for people committing code to projects hosted on svn.php.net or git.php.net. We will provide a full post mortem in due course, most likely next week. You can also get updates from the official php.net Twitter: @official_php. 24 Oct 2013 A quick update on the status of php.net On 24 Oct 2013 06:15:39 +0000 Google started saying www.php.net was hosting malware. The Google Webmaster Tools were initially quite delayed in showing the reason why and when they did it looked a lot like a false positive because we had some minified/obfuscated javascript being dynamically injected into userprefs.js. This looked suspicious to us as well, but it was actually written to do exactly that so we were quite certain it was a false positive, but we kept digging. It turned out that by combing through the access logs for static.php.net it was periodically serving up userprefs.js with the wrong content length and then reverting back to the right size after a few minutes. This is due to an rsync cron job. So the file was being modified locally and reverted. Google's crawler caught one of these small windows where the wrong file was being served, but of course, when we looked at it manually it looked fine. So more confusion. We are still investigating how someone caused that file to be changed, but in the meantime we have migrated www/static to new clean servers. The highest priority is obviously the source code integrity and after a quick: git fsck --no-reflog --full --strict on all our repos plus manually checking the md5sums of the PHP distribution files we see no evidence that the PHP code has been compromised. We have a mirror of our git repos on github.com and we will manually check git commits as well and have a full post-mortem on the intrusion when we have a clearer picture of what happened. Older News Entries Upgrading to PHP5.5 Upcoming conferencesPHP Conference ArgentinaDutch PHP Conference 2014php[tek] 2014 : Schedule AnnouncedInternational PHP Conference - Spring Edition - 2014 User Group Events Special Thanks Copyright © 2001-2014 The PHP Group My PHP.net Contact Other PHP.net sites Mirror sites Privacy policy", "klzzzr");
